package org.ypx.center.service.impl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ypx.center.service.ITaskService;
import org.ypx.center.service.IUserInfoService;
import org.ypx.center.service.IUserRoleService;
import org.ypx.center.service.IUserTaskService;
import org.ypx.db.entity.*;
import org.ypx.db.mapper.UserRoleMapper;
import org.ypx.shared.common.Result;
import org.ypx.shared.service.vo.TaskVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tingwen on 2017/3/17.
 */
@Service
public class TaskServiceImpl extends BaseServiceImpl<Task> implements ITaskService {
    @Autowired
    IUserTaskService userTaskService;

    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    IUserRoleService userRoleService;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public Result<List<Task>> selectByUserTask(UserTask userTask) {
        Result<List<UserTask>> result1 = userTaskService.select(userTask);
        List<UserTask> userTasks = result1.getData();
        List<Task> tasks = new ArrayList<>();
        userTasks.forEach(userTask1 -> {
            Task task = mapper.selectByPrimaryKey(userTask1.getTaskId());
            tasks.add(task);
        });
        result.setData(tasks);
        return result;
    }

    @Override
    public Result<List<Task>> saveByUserInfo(Task entity, UserInfo userInfo) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        entity.setUserId(user.getUserId());
        entity.setGenTime(new Date());
        this.save(entity);
        entity.setGenTime(null);
        entity = mapper.selectOne(entity);

        List<UserInfo> userInfos = userInfoService.select(userInfo).getData();
        UserTask userTask = new UserTask();
        userTask.setTaskId(entity.getTaskId());
        userInfos.forEach(userInfo1 -> {
            if (userInfo1 != null) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userInfo1.getUserId());
                List<UserRole> userRoles = userRoleService.select(userRole).getData();
                if (userRoles.get(0).getRoleId() == 4) {
                    userTask.setUserId(userInfo1.getUserId());
                    userTaskService.save(userTask);
                }
            }
        });
        return result;
    }

    @Override
    public Result<List<TaskVO>> selectTaskList(Task task) {
        List<Task> tasks = this.select(task).getData();
        List<TaskVO> taskVOs = new ArrayList<>();
        tasks.forEach(task1 -> {
            TaskVO taskVO = getTaskVO(task1);
            taskVOs.add(taskVO);
        });
        Result<List<TaskVO>> result = new Result<>();
        result.setData(taskVOs);
        return result;
    }

    @Override
    public Result<List<TaskVO>> selectTaskList(Task task, UserTask userTask) {
        List<UserTask> userTasks = userTaskService.select(userTask).getData();
        List<TaskVO> taskVOs = new ArrayList<>();
        userTasks.forEach(userTask1 -> {
            Task task1 = mapper.selectByPrimaryKey(userTask1.getTaskId());
            TaskVO taskVO = getTaskVO(task1);
            taskVOs.add(taskVO);
        });
        Result<List<TaskVO>> result = new Result<>();
        result.setData(taskVOs);
        return result;
    }


    public TaskVO getTaskVO(Task task) {
        TaskVO taskVO = new TaskVO();
        if (task != null) {
            taskVO.setTaskId(task.getTaskId());
            taskVO.setUserId(task.getUserId());
            taskVO.setDescription(task.getDescription());
            taskVO.setStartTime(task.getStartTime());
            taskVO.setGenTime(task.getGenTime());
            taskVO.setTitle(task.getTitle());
            taskVO.setEndTime(task.getEndTime());
        }
        taskVO.setProgress(userTaskService.getProgress());
        return taskVO;
    }

}
